package com.hhz.lambda.part1;

import org.junit.Test;

import java.util.*;

/**
 * @Author Rem
 * @Date 2020-02-10
 */

public class TestLambda {

    //匿名内部类
    @Test
    public void Test1() {
        Comparator<Integer> com = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        };

        TreeSet<Integer> set = new TreeSet<>(com);
    }

    //lambda表达式
    @Test
    public void Test2() {
        Comparator<Integer> com = (o1, o2) -> Integer.compare(o1, o2);
        TreeSet<Integer> set = new TreeSet<>(com);
/*        set.add(1);
        set.add(2);
        System.err.println(set.stream().max(com));*/
    }


    List<Employee> employees = Arrays.asList(
            new Employee("张三", 16, 3468.12),
            new Employee("李四", 42, 9485.32),
            new Employee("王五", 24, 8482.99),
            new Employee("赵六", 33, 6381.82),
            new Employee("田七", 37, 4448.45),
            new Employee("方八", 36, 7485.42)
    );

    //需求：获得当前公司员工年龄大于35的员工信息
    @Test
    public void test3() {
        List<Employee> list = filterEmployees(employees);
        for (Employee employee : list) {
            System.err.println(employee);
        }
        System.out.println("--------------------");

        List<Employee> list2 = filterEmployees2(employees);
        for (Employee employee : list2) {
            System.err.println(employee);
        }
    }

    public List<Employee> filterEmployees(List<Employee> list) {
        List<Employee> emps = new ArrayList<>();
        for (Employee emp : list) {
            if (emp.getAge() >= 35) {
                emps.add(emp);
            }
        }
        return emps;
    }

    //需求：获得当前公司员工工资大于5000并且的员工信息
    public List<Employee> filterEmployees2(List<Employee> list) {
        List<Employee> emps = new ArrayList<>();
        for (Employee emp : list) {
            if (emp.getSalary() > 5000) {
                emps.add(emp);
            }
        }
        return emps;
    }

    //代码重复度比较高需要优化
    //优化方式1：策略设计模式   写接口  使用特定的类继承接口实现功能
    @Test
    public void test4() {
        //按照年龄过滤
        List<Employee> list = filterEmployee(employees, new FilterEmployeeByAge());
        for (Employee employee : list) {
            System.out.println(employee);
        }
        System.out.println("----------------");
        //按照工资过滤
        List<Employee> list2 = filterEmployee(employees, new FilterEmployeeBySalary());
        for (Employee employee : list2) {
            System.out.println(employee);
        }
    }

    //只需要一个方法即可
    public List<Employee> filterEmployee(List<Employee> list, MyPredicate<Employee> mp) {
        List<Employee> emps = new ArrayList<>();

        for (Employee employee : list) {
            if (mp.test(employee)) {
                emps.add(employee);
            }
        }
        return emps;
    }

    //优化方式二：匿名内部类
    @Test
    public void test5() {
        //按照年龄过滤
        List<Employee> list = filterEmployee(employees, new MyPredicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return employee.getAge() > 35;
            }
        });

        for (Employee employee : list) {
            System.err.println(employee);
        }
    }

    //优化方式三：Lambda表达式：即是一段代码的传递
    @Test
    public void test6() {
        //按照工资过滤
        List<Employee> list = filterEmployee(employees, e -> e.getSalary() > 5000);
        list.forEach(System.out::println);
    }

    //优化方式四：Stream API
    @Test
    public void test7() {
        //按照工资过滤 取前两个
        employees.stream().filter(e -> e.getSalary() > 5000)
                .limit(2)
                .forEach(System.out::println);


        //提取所以人的名字
        employees.stream().map(Employee::getName)
                .forEach(System.out::println);
    }
}
